<?php
/**
 * SHOP 控制台首页
 * ============================================================================
 * 网络科技有限公司，并保留所有权利。
*/

define('IN_ZYS', true);
require(dirname(__FILE__) . '/includes/init.php');

//分类列表
if($_REQUEST['act'] == 'list'){
	$list = $db->getAll("SELECT * FROM " .$zys->table('nav'));
	/*分页开始*/
	$sql="select id,name,ifshow,opennew,vieworder,type from ".ly()."nav ";
	$num='10';
	$res=$db->query($sql);
	$total_count=$db->num_rows($res);
	$pagelist = new page(array('total'=>$total_count, 'perpage'=>$num));
	$currenpage=$pagelist->nowindex;
	$start=($currenpage-1)*$num;
	if ($total_count>$num){
		$smarty->assign('page',$pagelist->show(3));
	}else{
		$smarty->assign('page','');
	}
	$limit=" limit ".$start.','.$num;
	/*分页结束*/
	$sql.=$limit;
	$row=$db->getAll($sql);
    $smarty->assign('nav_list', $row);
    $smarty->display('nav_list.html');

}elseif($_REQUEST['act']=='add'){
	$id = isset($_REQUEST['id'])?$_REQUEST['id']:0;//导航id
	if($id){
		$sql="select id,name,ifshow,vieworder,opennew,type,url,vieworder from ".ly()."nav where id='{$id}'";
		@$nav=$db->getROW($sql);
		$smarty->assign('edit',$nav);
	}
	$smarty->display('nav_edit.html');
}elseif($_REQUEST['act']=='edit'){
	if($_POST['name_aj']){
		$nav=$db->getOne("select COUNT(*) from ".ly()."nav where name='{$_POST['name_aj']}'");
		echo $nav;//ajax判断用户名是否存在查询语句
		exit;//不可删除---------------------------
	}
	$id = isset($_REQUEST['id'])?$_REQUEST['id']:0;//导航id
	if($id){		
		$sql="update ".ly()."nav set name='{$_POST['name']}',ifshow='{$_POST['ifshow']}',opennew='{$_POST['opennew']}',type='{$_POST['type']}',vieworder='{$_POST['vieworder']}' where id='{$id}'";
	}else{
		$nav=$db->getOne("select COUNT(*) from ".ly()."nav where name='{$_POST['name']}'");
		if($nav=='1'){
			echo "<script>alert('名称已存在');history.go(-1);</script>";
			exit;//如果账号存在返回上一页
		}
		$sql="insert into ".ly()."nav (name,ifshow,opennew,type,url,vieworder) values ('{$_POST['name']}','{$_POST['ifshow']}','{$_POST['opennew']}','{$_POST['type']}','{$_POST['url']}','{$_POST['vieworder']}')";
	}
	$res=mysql_query($sql);
	if($res==1){
        $href[]=array('text'=>"返回导航拦列表",'href'=>'nav_list.php?act=list');
        sys_msg("操作成功！",$href);
    }else{
        $href[]=array('text'=>"重新编辑",'href'=>"history.go(-1);");
        sys_msg("操作失败！",$href,false);
    }
    $smarty->display('nav_list.html');
}
elseif($_REQUEST['act']=='ads_del'){
	$id = isset($_REQUEST['id'])?$_REQUEST['id']:0;//导航id
	if ($id) {
		$sql="delete from ".ly()."nav where id='{$id}' ";
	}
	$res=mysql_query($sql);
	if($res==1){
        $href[]=array('text'=>"返回导航拦列表",'href'=>'nav_list.php?act=list');
        sys_msg("操作成功！",$href);
    }
}elseif($_REQUEST['act'] == 'ajax_enabled'){
	//显示状态
    $id=isset($_POST['id'])?$_POST['id']:0;
    $ifshow=isset($_POST['show'])?$_POST['show']:0;
    if($ifshow==1){
    	$ifshow='0';
	}else{
		$ifshow='1';
	}
    $sql="update ".ly()."nav set ifshow='{$ifshow}' where id='{$id}'";
    $res=$db->query($sql);
    if($res){
        echo "1";  
    }else{
        echo "2";
    }
}
elseif($_REQUEST['act'] == 'ajax_new'){
	//显示状态
    $id=isset($_POST['id'])?$_POST['id']:0;
    $new=isset($_POST['news'])?$_POST['news']:0;
    if($new==1){
    	$new='0';
	}else{
		$new='1';
	}
    $sql="update ".ly()."nav set opennew='{$new}' where id='{$id}'";
    $res=$db->query($sql);
    if($res){
        echo "1";  
    }else{
        echo "2";
    }
}
?>